package dse.messages.locatedataobj;

import java.util.LinkedList;

import dse.Cluster;
import dse.DataObject;
import dse.Experiment;
import dse.ExperimentRecorder;
import dse.messages.Message;
import dse.messages.MessageHandler;
import dse.messages.MessagePurpose;
import dse.messages.MessageSender;
import dse.messages.MessageUtil;

public class LocateClustersWithDataObjectMessageHandler
extends MessageSender
implements MessageHandler
{
	
	private DataObject dobj;
	private MessagePurpose purpose;
	private LocateClustersWithDataObjectRequester requester;
	
	
	public LocateClustersWithDataObjectMessageHandler (
			Experiment experiment, 
			DataObject dobj,
			MessagePurpose purpose,
			LocateClustersWithDataObjectRequester requester
			)
	{
		super(experiment);
		
		this.dobj = dobj;
		this.purpose = purpose;
		this.requester = requester;
	}



	
	public boolean acceptMessage (
			Message message
			)
	{
		return true;
	}
	
	public void messageReceived (
			ExperimentRecorder recorder, 
			Message message,
			double timeLeft
			)
	{
		recorder.debug(this, "msgRcvd: " + message, timeLeft);
		
		LinkedList<Cluster> clusters = 
			MessageUtil.getClustersThatMayHaveDataObjectV2(
				experiment.network(), 
				message.to(),
				dobj, 
				experiment.modules().assigner()
				);
		
		
		Message m = super.createClustersThatMightHaveDataObjectMessage(
				message.to(), message.from(), clusters, purpose, requester
				);
		recorder.debug(this, "new msg: " + m, timeLeft);
		//experiment.messageQueue().enqueueMessageInPeriod(m, timeLeft);
	}
	
	public void messageInterupted (
			ExperimentRecorder recorder,
			Message message, 
			double timeLeft
			)
	{
		recorder.debug(this, "msgIntrpt: " + message, timeLeft);
		
		requester.clustersThatMightHaveDataObject(recorder, null, timeLeft);
	}
}
